﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;
using System.Windows.Threading;
using QuanLyTiemVang.GUI;
using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;

namespace QuanLyTiemVang
{    
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window , IPhanQuyenHelper
    {
        private const int TIME_TO_COUNT_IDLE = 0; // second        
        private int TIME_TO_LOCK;

        private DispatcherTimer timer;
        private int count = 0;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;            
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {            
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += timer_Tick;
            timer.Start();
            this.displayTimeToScreen();
            this.lockScreen();        
    
            // codes to unlock
            TAIKHOAN tk = new TAIKHOAN();
            tk.TenDangNhap = "useradmin";
            tk = TaiKhoanDAO.SelectTaiKhoanByTenDangNhap(tk);
            CurrentUser.Instance.SignIn(tk);
            CurrentUser.Instance.AddForm(this);
            this.unLockScreen();

            TIME_TO_LOCK = (int)QuiDinhDAO.GetTimeToLock().GiaTri;
        }

        // increase time and count idle time
        // if idle time> TIME_TO_LOCK, call lockScreen function
        private void timer_Tick(object sender, EventArgs e)
        {
            this.displayTimeToScreen();
            count += 1;
            if (count > TIME_TO_COUNT_IDLE)
            {
                count = 0;
                long idleTime = IdleTimeFinder.GetIdleTime() / 1000;
                if (idleTime > TIME_TO_LOCK)
                    lockScreen();
            }
        }

        // Display Time to Screen
        private void displayTimeToScreen()
        {
            int hour = DateTime.Now.Hour;
            int minute = DateTime.Now.Minute;
            int second = DateTime.Now.Second;
            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            this.lbHM.Content = hour + ":" + minute + ":" + second;
            this.lbY.Content = day + "-" + month + "-" + year;
        }

        // Lock Screen
        private void lockScreen()
        {
            this.btnPhieuNhapKho.Visibility = Visibility.Hidden;
            CurrentUser.Instance.SignOut();
            this.ribbon.Visibility = Visibility.Collapsed;
            this.btnLock.Visibility = Visibility.Visible;
            this.lbPassword.Visibility = Visibility.Hidden;
            this.lbUserName.Visibility = Visibility.Hidden;
            this.txtPassword.Visibility = Visibility.Hidden;
            this.txtUserName.Visibility = Visibility.Hidden;
            this.lbUserDisplay.Visibility = Visibility.Hidden;
            this.btnLogOut.Visibility = Visibility.Hidden;
        }

        // UnLock Screen
        private void unLockScreen()
        {
            this.btnPhieuNhapKho.Visibility = Visibility.Visible;
            this.ribbon.Visibility = Visibility.Visible;
            this.lbPassword.Visibility = Visibility.Hidden;
            this.lbUserName.Visibility = Visibility.Hidden;
            this.txtPassword.Visibility = Visibility.Hidden;
            this.txtUserName.Visibility = Visibility.Hidden;
            this.lbUserDisplay.Visibility = Visibility.Visible;
            this.btnLogOut.Visibility = Visibility.Visible;
            this.btnLock.Visibility = Visibility.Hidden;
            this.lbUserDisplay.Content = CurrentUser.Instance.TaiKhoan.TenDangNhap;
        }       

        // Sign Out
        private void btnLogOut_Click_1(object sender, RoutedEventArgs e)
        {
            this.lockScreen();
            CurrentUser.Instance.SignOut();            
        }

        // Sign In
        private void txtPassword_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (string.IsNullOrEmpty(txtUserName.Text) == true)
                    this.txtUserName.Focus();
                else if (string.IsNullOrEmpty(txtPassword.Password) == true)
                    this.txtPassword.Focus();
                else if (txtPassword.Password.Length < 8)
                {
                    MessageBox.Show("Password phải có độ dài trong khoảng 8-255 kí tự.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                    this.txtPassword.Clear();
                    this.txtPassword.Focus();
                }
                else if (txtUserName.Text.Length < 8)
                {
                    MessageBox.Show("Tên đăng nhập có độ dài trong khoảng 8-50 kí tự.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                    this.txtUserName.Clear();
                    this.txtUserName.Focus();
                }
                else
                {
                    // check log in information
                    if (string.IsNullOrEmpty(txtUserName.Text) == true
                        || string.IsNullOrEmpty(txtPassword.Password) == true)
                    {
                        MessageBox.Show("Chưa nhập tên đăng nhập và mật khẩu.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        TAIKHOAN tk = new TAIKHOAN() { TenDangNhap = txtUserName.Text, MatKhau = txtPassword.Password };
                        // check if success log in
                        if (TaiKhoanDAO.IsRightPassword(tk) == true)
                        {
                            CurrentUser.Instance.SignIn(TaiKhoanDAO.SelectTaiKhoanByTenDangNhap(tk));
                            CurrentUser.Instance.AddForm(this);
                            this.unLockScreen();
                            this.txtPassword.Clear();
                            this.txtUserName.Clear();
                        }
                        else
                        {
                            MessageBox.Show("Tên đăng nhập hoặc mật khẩu không đúng.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                            this.txtPassword.Clear();
                        }
                    }
                }
            }
        }

        #region xu li su kien

        private void tbnPhieuNhapKho_Click_1(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            PhieuNhapKho pnk = new PhieuNhapKho();
            pnk.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnQuiDinh_Click_1(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            QuiDinh qd = new QuiDinh();
            qd.OnChangeTimeToLock += qd_OnChangeTimeToLock;
            qd.ShowDialog();
            this.Visibility = Visibility.Visible;
            
        }

        // call when TimeToLock changed
        private void qd_OnChangeTimeToLock()
        {
            TIME_TO_LOCK = (int)QuiDinhDAO.GetTimeToLock().GiaTri;
        }

        private void Log_In_Click_EventHanlder(object sender, RoutedEventArgs e)
        {
            if (lbPassword.Visibility == Visibility.Hidden)
            {
                this.lbPassword.Visibility = Visibility.Visible;
                this.lbUserName.Visibility = Visibility.Visible;
                this.txtPassword.Visibility = Visibility.Visible;
                this.txtUserName.Visibility = Visibility.Visible;
                this.txtUserName.Focus();
                return;
            }
        }        
        private void btnDanhSachSanPham_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            DanhSachSanPham ds = new GUI.DanhSachSanPham();
            ds.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnThoGiaCong_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            DanhSachThoGiaCong ds = new GUI.DanhSachThoGiaCong();
            ds.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnPhieuBanHang_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            PhieuBanHang pbh = new GUI.PhieuBanHang();
            pbh.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnPhieuNo_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            PhieuNo pn = new PhieuNo();
            pn.SetPhieuNoForBanHang(false);
            pn.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnPhieuMuaHang_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            PhieuMuaHang mh = new PhieuMuaHang();
            mh.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnPhieuDichvu_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            PhieuDichVu pdv = new PhieuDichVu();
            pdv.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnPhieuGiacong_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            PhieuHangGiaCong gc = new PhieuHangGiaCong();
            gc.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnPhieuChi_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            PhieuChi pc = new PhieuChi();
            pc.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnBaoCaoDoanhThu_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            BaoCaoDoanhThu bcdt = new BaoCaoDoanhThu();
            bcdt.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnBaoCaoton_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            BaoCaoTonKho bctk = new BaoCaoTonKho();
            bctk.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnTraCuuSanPham_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            TraCuuSanPham tcsp = new TraCuuSanPham();
            tcsp.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnDanhSachKhachHang_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            DanhSachKhachHang dskh = new DanhSachKhachHang();
            dskh.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnTraCuuKhachHang_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            TraCuuKhachHang tckh = new TraCuuKhachHang();
            tckh.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnDanhSachDichVu_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            DanhSachDichVu dv = new DanhSachDichVu();
            dv.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnHangGiaCong_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            DanhSachHangGiaCong hgc = new DanhSachHangGiaCong();
            hgc.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnDangNhap_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            DangKyTaiKhoan dk = new DangKyTaiKhoan();
            dk.ShowDialog();
            this.Visibility = Visibility.Visible;
        }
      
        private void btnHuongDan_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            ThongTinUngDung tt = new ThongTinUngDung();
            tt.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #endregion
              
        #region implements IPhanQuyenHelper
        void IPhanQuyenHelper.SetAdminFunction()
        {
            btnDanhSachKhoanPhi.IsEnabled = true;
            btnDangNhap.IsEnabled = true;
            btnPhieuNhapKho.IsEnabled = true;
            btnQuiDinh.IsEnabled = true;
            btnBaoCaoDoanhThu.IsEnabled = true;
            btnBaoCaoton.IsEnabled = true;
            btnDangNhap.IsEnabled = true;
            btnDangXuat.IsEnabled = true;
            btnDanhSachDichVu.IsEnabled = true;
            btnDanhSachKhachHang.IsEnabled = true;
            btnDanhSachSanPham.IsEnabled = true;
            btnHangGiaCong.IsEnabled = true;
            btnHuongDan.IsEnabled = true;
            btnPhieuBanHang.IsEnabled = true;
            btnPhieuChi.IsEnabled = true;
            btnPhieuDichvu.IsEnabled = true;
            btnPhieuGiacong.IsEnabled = true;
            btnPhieuMuaHang.IsEnabled = true;
            btnPhieuNo.IsEnabled = true;
            btnThoat.IsEnabled = true;
            btnThoGiaCong.IsEnabled = true;
            btnTraCuuKhachHang.IsEnabled = true;
            btnTraCuuSanPham.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
            btnDanhSachKhoanPhi.IsEnabled = true;
            btnDangNhap.IsEnabled = false;
            btnPhieuNhapKho.IsEnabled = true;
            btnQuiDinh.IsEnabled = true;
            btnBaoCaoDoanhThu.IsEnabled = false;
            btnBaoCaoton.IsEnabled = false;
            btnDangNhap.IsEnabled = true;
            btnDangXuat.IsEnabled = true;
            btnDanhSachDichVu.IsEnabled = true;
            btnDanhSachKhachHang.IsEnabled = true;
            btnDanhSachSanPham.IsEnabled = true;
            btnHangGiaCong.IsEnabled = true;
            btnHuongDan.IsEnabled = true;
            btnPhieuBanHang.IsEnabled = true;
            btnPhieuChi.IsEnabled = true;
            btnPhieuDichvu.IsEnabled = true;
            btnPhieuGiacong.IsEnabled = true;
            btnPhieuMuaHang.IsEnabled = true;
            btnPhieuNo.IsEnabled = true;
            btnThoat.IsEnabled = true;
            btnThoGiaCong.IsEnabled = true;
            btnTraCuuKhachHang.IsEnabled = true;
            btnTraCuuSanPham.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
            btnDanhSachKhoanPhi.IsEnabled = false;
            btnDangNhap.IsEnabled = false;
            btnPhieuNhapKho.IsEnabled = false;
            btnQuiDinh.IsEnabled = false;
            btnBaoCaoDoanhThu.IsEnabled = false;
            btnBaoCaoton.IsEnabled = false;
            btnDangNhap.IsEnabled = false;
            btnDangXuat.IsEnabled = true;
            btnDanhSachDichVu.IsEnabled = true;
            btnDanhSachKhachHang.IsEnabled = false;
            btnDanhSachSanPham.IsEnabled = true;
            btnHangGiaCong.IsEnabled = false;
            btnHuongDan.IsEnabled = true;
            btnPhieuBanHang.IsEnabled = false;
            btnPhieuChi.IsEnabled = false;
            btnPhieuDichvu.IsEnabled = false;
            btnPhieuGiacong.IsEnabled = false;
            btnPhieuMuaHang.IsEnabled = false;
            btnPhieuNo.IsEnabled = false;
            btnThoat.IsEnabled = true;
            btnThoGiaCong.IsEnabled = false;
            btnTraCuuKhachHang.IsEnabled = true;
            btnTraCuuSanPham.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            btnDanhSachKhoanPhi.IsEnabled = false;
            btnQuiDinh.IsEnabled = false;
            btnBaoCaoDoanhThu.IsEnabled = false;
            btnBaoCaoton.IsEnabled = false;
            btnDangNhap.IsEnabled = false;
            btnDangXuat.IsEnabled = false;
            btnDanhSachDichVu.IsEnabled = false;
            btnDanhSachKhachHang.IsEnabled = false;
            btnDanhSachSanPham.IsEnabled = false;
            btnHangGiaCong.IsEnabled = false;
            btnHuongDan.IsEnabled = false;
            btnPhieuBanHang.IsEnabled = false;
            btnPhieuChi.IsEnabled = false;
            btnPhieuDichvu.IsEnabled = false;
            btnPhieuGiacong.IsEnabled = false;
            btnPhieuMuaHang.IsEnabled = false;
            btnPhieuNo.IsEnabled = false;
            btnThoat.IsEnabled = true;
            btnThoGiaCong.IsEnabled = false;
            btnTraCuuKhachHang.IsEnabled = false;
            btnTraCuuSanPham.IsEnabled = false;
        }
        #endregion                

        private void btnDanhSachKhoanPhi_Click_1(object sender, RoutedEventArgs e)
        {
            DanhSachKhoanPhi ds = new DanhSachKhoanPhi();
            ds.ShowDialog();
        }
    }    
}
